package com.cash.channel.web.mq;

import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.model.callback.ApplyAuditStatusCallBack;
import com.cash.channel.web.logic.CallBackLogic;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * Created by chengbingzheng on 2019/8/5.
 */
@Slf4j
@Component
public class QCOrderStatusListener {

    @Autowired
    private CallBackLogic callBackLogic;

    @RabbitListener(containerFactory = "rabbitListenerContainerFactory", bindings = {
            @QueueBinding(
                    // 绑定queue
                    value = @Queue(value = Constant.ProConstant.CHANNEL_QC_STATUS_NOTICE_CALLBACK, durable = "true", autoDelete = "false"),
                    // 绑定exchange
                    exchange = @Exchange(value = "geex", durable = "true"),
                    key = Constant.ProConstant.CHANNEL_QC_STATUS_NOTICE_CALLBACK)})
    public void receiveChannelProtocol(Channel channel, Message message) {
        try {
            // 实体接收
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
            // 字符串接收
            log.info("收到订单状态反馈通知：" + message);
            ApplyAuditStatusCallBack map = objectMapper.readValue(message.getBody(), ApplyAuditStatusCallBack.class);
            log.info("收到订单状态反馈通知 repayResultAmqp:" + map.toString());
            try {
                if (map != null) {
                    Integer isCustom = map.getIsCustom();
                    if (0 == isCustom) {
                        ApplyAuditStatusCallBack callBack = objectMapper.readValue(message.getBody(), ApplyAuditStatusCallBack.class);
                        log.info("获取认证状态信息："+ JSON.toJSONString(callBack));
                        callBackLogic.commonQcOrderStatusCallBack(map.getChannel(), callBack);
                    } else {
                        // TODO: 2019/11/5 第三方
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw e;
            }
            // 手动确认成功消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            try {
                // 异常情况，手动确认未消费成功，进入死信队列
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            } catch (IOException ioe) {
                log.error(ioe.getMessage(), ioe);
            }
        }
    }

}
